package com.su.common.handler;

import com.su.common.constants.ErrorCodeEnum;
import com.su.common.exception.DaoException;
import com.su.common.exception.ServiceException;
import com.su.common.exception.SysException;
import com.su.common.vo.ReturnResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @Author 大哥
 * @Description : 统一的异常处理器
 * @DateTime : 2021/12/15 10:05
 **/
@RestControllerAdvice
public class GlobalExceptionHandler {

    private static final Logger LOGGER = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    /*** 捕获异常 * @param e 异常类型为 Exception * @return */
    @ExceptionHandler(value = Exception.class) public ReturnResult error(Exception e) { LOGGER.error(e.getMessage(), e);
        return ReturnResult.error(ErrorCodeEnum.SYSTEM_EXECUTION_ERROR); }

    /*** 捕获异常 * @param e 异常类型为 ServiceException * @return */
    @ExceptionHandler(value = ServiceException.class) public ReturnResult error(ServiceException e) { LOGGER.error(e.getMessage(), e); return ReturnResult.error(e.getErrorCode(),e.getMessage()); }

    /*** 捕获异常 * @param e 异常类型为 DaoException * @return */
    @ExceptionHandler(value = DaoException.class) public ReturnResult error(DaoException e) { LOGGER.error(e.getMessage(), e); return ReturnResult.error(e.getErrorCode(),e.getMessage()); }

    @ExceptionHandler(value = SysException.class) public ReturnResult error(SysException e) { LOGGER.error(e.getMessage(), e); return ReturnResult.error(e.getErrorCode(), e.getMessage()); }
}
